package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.SiftKVP;
import com.elinkway.infinitemovies.bean.SubChannelType;
import com.elinkway.infinitemovies.utils.MoviesConstant;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class SiftKVPParser extends LVideoMobileParser<SubChannelType> {

	
	@Override
    public SubChannelType parse(JSONObject data) throws Exception {

        SubChannelType mSubChannelType = new SubChannelType();
        ArrayList<SiftKVP> firstKvps = new ArrayList<SiftKVP>();
        firstKvps.add(new SiftKVP("最热", MoviesConstant.HOTEST, "orderby"));
        firstKvps.add(new SiftKVP("最新", MoviesConstant.LASTEST, "orderby"));
        firstKvps.add(new SiftKVP("好评", MoviesConstant.GOODPING, "orderby"));
        mSubChannelType.addChannelSubList(firstKvps);
        JSONArray all_arr = data.getJSONArray("data");
        for (int j = 0; j < all_arr.length(); j++) {
            ArrayList<SiftKVP> subcategory_kvps = new ArrayList<SiftKVP>();
            JSONObject sub_obj = all_arr.getJSONObject(j);
            JSONArray sub_data_arr = sub_obj.getJSONArray("filterData");
            for (int i = 0; i < sub_data_arr.length(); i++) {
                JSONObject sub_data_obj = sub_data_arr.getJSONObject(i);
                SiftKVP kvp = new SiftKVP();
                kvp.setFilterKey(sub_obj.optString("filter"));
                kvp.setKey(sub_data_obj.getString("name"));
                kvp.setId(sub_data_obj.getString("id"));
                subcategory_kvps.add(kvp);
            }
            mSubChannelType.addChannelSubList(subcategory_kvps);
        }
        return mSubChannelType;
    }
}
